package net.diaowen.dwsurvey.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import net.diaowen.common.base.entity.User;
import net.diaowen.common.base.service.AccountManager;
import net.diaowen.common.plugs.page.Page;
import net.diaowen.dwsurvey.entity.LotteryUser;
import net.diaowen.dwsurvey.entity.LotteryUserDetail;
import net.diaowen.dwsurvey.entity.PrizeType;
import net.diaowen.dwsurvey.entity.StudentLottery;
import net.diaowen.dwsurvey.service.LotteryPrizeManager;
import net.diaowen.dwsurvey.service.LotteryUserManager;
import net.diaowen.dwsurvey.service.StudentLotteryManager;

@Controller
@RequestMapping("/sy/lottery/user")
public class LotteryUserController{
	
	@Autowired
	private AccountManager accountManager;
	
	@Autowired
	private StudentLotteryManager studentLotteryManager;
	
	@Autowired
	private LotteryUserManager lotteryUserManager;
	
	@Autowired
	private LotteryPrizeManager lotteryPrizeManager;
// 中奖成员——问卷下的人
	@RequestMapping("/list.do")
	public ModelAndView list(HttpServletRequest request,Page<StudentLottery> page,StudentLottery entity,String id) throws Exception {
		if (id != null) {
			entity.setLotteryActivityId(id);
		}
		ModelAndView modelAndView = new ModelAndView();
		List<PrizeType> prizeTypeList = lotteryPrizeManager.getPrizeTypeList();
		page=studentLotteryManager.findPage(page,entity);
		modelAndView.addObject("prizeTypeList", prizeTypeList);
		modelAndView.addObject("page",page);
		modelAndView.addObject("entity",entity);

		modelAndView.setViewName("/diaowen-lottery/user-list");
		return modelAndView;
	}
	
	
	/**
	 * 问卷添加小组
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save.do")
	public String save(LotteryUserDetail entity,String approvalStatus,String lotteryActivityId) throws Exception {
		try{
			String id = entity.getId();
			LotteryUser lu = lotteryUserManager.get(entity.getLotteryUserId());
			lu.setApprovalStatus(approvalStatus);
			User curUser = accountManager.getCurUser();
			lu.setApprovalTime(new Date());
			lu.setApprovalUser(curUser.getId());
			lotteryUserManager.save(lu);
			if(id==null || id.equals("")){
				entity.setId(null);
				lotteryUserManager.saveLotteryUserDetail(entity);
			}else{
				LotteryUserDetail lud = lotteryUserManager.getLotteryUserDetail(id);
				lud.setAddress(entity.getAddress());
				lud.setDeliveryNo(entity.getDeliveryNo());
				lud.setItemNo(entity.getItemNo());
				lud.setName(entity.getName());
				lud.setPhone(entity.getPhone());
				lud.setRemark(entity.getRemark());
				lud.setZipcode(entity.getZipcode());
				lotteryUserManager.saveLotteryUserDetail(lud);
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return "redirect:/sy/lottery/user/list.do?lotteryActivityId="+lotteryActivityId;
	}

	@RequestMapping("/input.do")
	public ModelAndView input(String lauId,String wxId,String lotteryActivityId) throws Exception {
		LotteryUserDetail lud = lotteryUserManager.initApprovalPage(lauId, wxId);
	    LotteryUser lu = lotteryUserManager.get(lauId);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("lotteryActivityId",lotteryActivityId);
		modelAndView.addObject("lu",lu);
		modelAndView.addObject("entity",lud);
		modelAndView.setViewName("/diaowen-lottery/user-input");
		return modelAndView;
	}

}
